//
//  PersonJobRespView.swift
//  ANOASTORE
//
//  Created by hudan on 2025/7/30.
//  Copyright © 2025 zwb. All rights reserved.
//

import UIKit

class PersonJobRespView: UIView {
    
    private let contentView = UIView().extBgColor(color: .white)
        .extRadius(radius: 10).extMaskBounds(mask: true)
        .extFrame(frame: CGRect(x: 30, y: KS_SCREEN_HEIGHT, width: KS_SCREEN_WIDTH - 2*30, height: KS_SCREEN_HEIGHT*0.6))
    
    private let titleLab = UILabel().easy(UIColor(kshex: "#333333"), .systemFont(ofSize: 20, weight: .medium))
    
    private lazy var textView: UITextView = {
        let view = UITextView()
        view.font = .systemFont(ofSize: 16, weight: .medium)
        view.textColor = UIColor(kshex: "#444444")
        view.isEditable = false
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() {
        extBgColor(color: .init(white: 0, alpha: 0.4))
        
        let touchBtn = UIButton().easy(nil, self, #selector(hide))
        addSubview(touchBtn)
        touchBtn.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets.zero)
        }
        
        addSubview(contentView)
        
        contentView.addSubview(titleLab)
        titleLab.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.height.equalTo(24)
            make.top.equalToSuperview().offset(15)
        }
        
        let closeBtn = UIButton().easy("close_x", self, #selector(hide))
        closeBtn.imageEdgeInsets = .init(top: 10, left: 10, bottom: 10, right: 10)
        contentView.addSubview(closeBtn)
        closeBtn.snp.makeConstraints { make in
            make.width.height.equalTo(40)
            make.top.right.equalToSuperview()
        }
        
        let line = UIView().extBgColor(color: UIColor(kshex: "#cccccc"))
        contentView.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.right.equalTo(titleLab)
            make.top.equalTo(titleLab.snp.bottom).offset(15)
            make.height.equalTo(1)
        }
        
        contentView.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.top.equalTo(line.snp.bottom).offset(10)
            make.bottom.equalToSuperview().offset(-10)
        }
    }
    
    @objc private func hide() {
        UIView.animate(withDuration: 0.3) {
            self.contentView.ks_y = KS_SCREEN_HEIGHT
            self.contentView.alpha = 0.05
        } completion: { _ in
            UIView.animate(withDuration: 0.2) {
                self.alpha = 0.05
            } completion: { _ in
                self.removeFromSuperview()
            }
        }
    }
    
    func show(WithJob job: String, jobResp: String) {
        if let keyWindow = UIApplication.shared.windows.filter({ $0.isKeyWindow }).first {
            
            titleLab.text = "\(job)岗位职责"
            
            textView.text = jobResp
            
            keyWindow.addSubview(self)
            UIView.animate(withDuration: 0.3) {
                self.contentView.ks_centerY = self.ks_centerY
            }
        }
    }
}
